package com.pactera.madp.cp.service.sync.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.sync.SyncCustTagInfoHisEntity;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.mapper.sync.SyncCustTagInfoHisMapper;
import com.pactera.madp.cp.service.sync.SyncCustTagInfoHisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SyncCustTagInfoHisServiceImpl extends ServiceImpl<SyncCustTagInfoHisMapper, SyncCustTagInfoHisEntity> implements SyncCustTagInfoHisService {

    @Override
    public void syncOneDay(String syncDate) {
        // 插入历史表前，删除可能存在的上一天数据
        this.deleteOneDate(syncDate);
        // 同步上一天数据到历史表
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_SYNC_TAG_INFO_HIS, Constants.TC_TAG_INFO);
        tableNames.forEach(t -> {
            String tableIndex = t.substring(t.length() - 1);
            baseMapper.syncData(tableIndex, syncDate);
        });
    }

    @Override
    public void deleteOneDate(String date) {
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_SYNC_TAG_INFO_HIS, Constants.TC_TAG_INFO);
        tableNames.forEach(t -> baseMapper.deleteOneDate(t));
    }

}
